package org.madgecko.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Author       : MadFrog
 * Date         : 2019/2/2 12:02
 * Description  : Android竞赛模拟试卷A接口
 */
@Controller
@RequestMapping("/androidCompetitionSimulateAlpha")
public class AndroidCompetitionSimulateAlphaController {

    /**
     * 查询小车的当前速度
     *
     * @param CarId 小车编号：1-3
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/GetCarSpeed", method = RequestMethod.POST)
    public ServerInfo loadCarSpeed(Integer CarId) {
        if (CarId != null && CarId >= 1 && CarId <= 3) {
            HashMap hashMap = new HashMap();
            int speed = (int) (1 + Math.random()*(17+1-1) + 1);
            hashMap.put("CarSpeed", speed);
            return new ServerInfo(hashMap);
        } else {
            HashMap hashMap = new HashMap();
            hashMap.put("result", "failed");
            return new ServerInfo(hashMap);
        }
    }

    /**
     * 查询所有传感器的值
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/GetAllSense", method = RequestMethod.POST)
    public ServerInfo loadAllSense() {
        HashMap hashMap = new HashMap();
        int pm25 = (int) (1 + Math.random()*(150+1-30) + 30);
        int co2 = (int) (1 + Math.random() *(7000+1-4000) + 4000);
        int lightIntensity = (int) (1 + Math.random()*(2000+1-1000) + 1000);
        int humidity = (int) (1 + Math.random()*(80+1-30) + 30);
        int temperature = (int) (1 + Math.random()*(45+1-0) + 0);
        hashMap.put("pm2.5", pm25);
        hashMap.put("co2", co2);
        hashMap.put("LightIntensity", lightIntensity);
        hashMap.put("humidity", humidity);
        hashMap.put("temperature", temperature);
        return new ServerInfo(hashMap);
    }

    /**
     * 查询道路拥挤状态
     * @param RoadId 1-12(道路编号)
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/GetRoadStatus", method = RequestMethod.POST)
    public ServerInfo loadRoadStatus(Integer RoadId) {
        if (RoadId != null && RoadId >= 1 && RoadId <= 12) {
            HashMap hashMap = new HashMap();
            int status = (int) (1 + Math.random()*(5+1-0) + 0);
            hashMap.put("Status", status);
            return new ServerInfo(hashMap);
        } else {
            HashMap hashMap = new HashMap();
            hashMap.put("result", "failed");
            return new ServerInfo(hashMap);
        }
    }

    /**
     * 查询道路拥挤状态
     * @param BusStationId 站台编号：1-2
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/GetBusStationInfo", method = RequestMethod.POST)
    public ServerInfo loadBusStationInfo(Integer BusStationId) {
        if (BusStationId != null && BusStationId >= 1 && BusStationId <= 2) {
            HashMap hashMapOne = new HashMap();
            int busOneDistance = (int) (1 + Math.random()*(120000+1-1) + 1);
            hashMapOne.put("Distance", busOneDistance);
            hashMapOne.put("BusId", 1);
            HashMap hashMapTwo = new HashMap();
            int busTwoDistance = (int) (1 + Math.random()*(120000+1-1) + 1);
            hashMapTwo.put("Distance", busTwoDistance);
            hashMapTwo.put("BusId", 1);
            List<HashMap> lm = new ArrayList<HashMap>();
            lm.add(hashMapOne);
            lm.add(hashMapTwo);
            return new ServerInfo(lm);
        } else {
            HashMap hashMap = new HashMap();
            hashMap.put("result", "failed");
            return new ServerInfo(hashMap);
        }
    }

    /**
     * 查询小车的账户余额
     *
     * @param CarId 小车编号：1-3
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/GetCarAccountBalance", method = RequestMethod.POST)
    public ServerInfo loadCarAccountBalance(Integer CarId) {
        if (CarId != null && CarId >= 1 && CarId <= 3) {
            HashMap hashMap = new HashMap();
            int balance = (int) (1 + Math.random()*(10000+1-1) + 1);
            hashMap.put("Balance", balance);
            return new ServerInfo(hashMap);
        } else {
            HashMap hashMap = new HashMap();
            hashMap.put("result", "failed");
            return new ServerInfo(hashMap);
        }
    }

    /**
     * 小车账户充值
     *
     * @param CarId 小车编号：1-3
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/SetCarAccountRecharge", method = RequestMethod.POST)
    public ServerInfo updateCarAccountRecharge(Integer CarId, Integer Money) {
        if (CarId != null && CarId >= 1 && CarId <= 3 && Money != null && Money <= 10000) {
            HashMap hashMap = new HashMap();
            hashMap.put("result", "ok");
            return new ServerInfo(hashMap);
        } else {
            HashMap hashMap = new HashMap();
            hashMap.put("result", "failed");
            return new ServerInfo(hashMap);
        }
    }

    /**
     * 查询光照传感器的阀值
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/GetLightSenseValve", method = RequestMethod.POST)
    public ServerInfo loadLightSenseValve() {
        HashMap hashMap = new HashMap();
        int down = (int) (1 + Math.random()*(1500+1-500) + 1);
        int up = (int) (1 + Math.random()*(4000+1-2000) + 1);
        hashMap.put("Down", down);
        hashMap.put("Up", up);
        return new ServerInfo(hashMap);
    }

    /**
     * 查询当前空闲车位
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/GetParkFree", method = RequestMethod.POST)
    public ServerInfo loadParkFree() {
        HashMap hashMap = new HashMap();
        int parkFreeId = (int) (1 + Math.random()*(10+1-1) + 1);
        hashMap.put("ParkFreeId", parkFreeId);
        return new ServerInfo(hashMap);
    }

    /**
     * 接口json返回对象
     */
    private class ServerInfo {
        private Object serverinfo;

        public ServerInfo() {
        }

        public ServerInfo(Object serverinfo) {
            this.serverinfo = serverinfo;
        }

        public Object getServerinfo() {
            return serverinfo;
        }

        public void setServerinfo(Object serverinfo) {
            this.serverinfo = serverinfo;
        }
    }
}
